﻿using CipherFactory;
using IR.Common.Utilities;
using IR.Controller.ModelControllers;
using IR.WPF.UI.ViewModels;
using IR.WPF.UI.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IR.WPF.UI.Controls.LandingViews
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : UserControl
    {
        // They refuse ping requests on their actual DB's so we'll just see if the front end is up
        private static string host = "www.freemysqlhosting.net";

        public Login()
        {
            InitializeComponent();
        }

        private void login_Click(object sender, RoutedEventArgs e) {
            Button btn = sender as Button;
            MainWindow parentWindow = App.Current.MainWindow as MainWindow;
            string username;
            string userpassword;

            if(object.ReferenceEquals(btn, LoginButton)) {
                // See if host is there
                string connection = HeartBeat.PingHost(host);
                if (!(string.IsNullOrEmpty(connection)))
                {
                    MessageBox.Show(connection);
                }
                else
                {

                    //Real Values
                    //username = Username.Text;
                    //userpassword = Password.Password;

                    //Dev Values
                    username = "iradmin";
                    userpassword = "qwe123";

                    try
                    {
                        if (string.IsNullOrEmpty(userpassword) || string.IsNullOrEmpty(username))
                        {
                            Exception exception = new Exception();
                            throw exception;
                        }
                        parentWindow.LoggedInUser = new CUser(username, StringCipher.Encrypt(userpassword, Settings.GetSetting("cipherpass")));
                    }
                    catch
                    {
                        parentWindow.LoggedInUser = null;
                        Password.Password = "";
                        MessageBoxResult invalidUnamePass = MessageBox.Show("Invalid Username or Password.\nPlease try again.");
                    }

                    if (parentWindow.LoggedInUser != null)
                    {
                        CompositeControlHost.ShowWindow<Home>(new VMHome(), Window.GetWindow(btn));
                        Password.Password = "";
                        Window.GetWindow(btn).Hide();
                    }
                }
            }
        }
    }
}
